package com.zhuliwei.controller;

import com.alibaba.excel.EasyExcel;
import com.alibaba.fastjson.JSON;
import com.zhuliwei.domain.ResponseResult;
import com.zhuliwei.domain.dto.ChangeLinkStatusDto;
import com.zhuliwei.domain.dto.LinkDto;
import com.zhuliwei.domain.entity.Link;
import com.zhuliwei.domain.vo.LinkVo;
import com.zhuliwei.domain.vo.PageVo;
import com.zhuliwei.enums.AppHttpCodeEnum;
import com.zhuliwei.service.LinkService;
import com.zhuliwei.utils.BeanCopyUtils;
import com.zhuliwei.utils.WebUtils;
import io.swagger.annotations.Api;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 友链管理接口控制层
 *
 * @author 朱力维
 * @time 2022/10/27 20:23
 **/
@RestController
@RequestMapping("/content/link")
@Api(tags="友链",description = "友链管理相关接口")
public class LinkController {
    @Resource
    private LinkService linkService;
    @GetMapping("/list")
    @PreAuthorize("@ps.hasPermission('content:link:list')")
    public ResponseResult<PageVo> list(Integer pageNum, Integer pageSize, LinkDto linkDto){
        return linkService.pageLinkList(pageNum,pageSize,linkDto);
    }

    @PostMapping
    @PreAuthorize("@ps.hasPermission('content:link:add')")
    public ResponseResult addLink(@RequestBody LinkDto linkDto){
        if (!StringUtils.hasText(linkDto.getName())){
            return ResponseResult.errorResult(500,"名称不能为空");
        }
        if (!StringUtils.hasText(linkDto.getLogo())){
            return ResponseResult.errorResult(500,"logo地址不能为空");
        }
        if (!StringUtils.hasText(linkDto.getAddress())){
            return ResponseResult.errorResult(500,"网站地址不能为空");
        }
        if (!StringUtils.hasText(linkDto.getStatus())){
            return ResponseResult.errorResult(500,"状态不能为空");
        }
        Link link = BeanCopyUtils.copyBean(linkDto, Link.class);
        linkService.save(link);
        return ResponseResult.okResult();
    }

    @DeleteMapping("/{ids}")
    @PreAuthorize("@ps.hasPermission('content:link:delete')")
    public ResponseResult deleteLink(@PathVariable List<String> ids){
        linkService.removeByIds(ids);
        return ResponseResult.okResult();
    }

    @GetMapping("/{id}")
    public ResponseResult linkDetail(@PathVariable String id){
        Link link = linkService.getById(id);
        LinkVo linkVo = BeanCopyUtils.copyBean(link, LinkVo.class);
        return ResponseResult.okResult(linkVo);
    }

    @PutMapping
    @PreAuthorize("@ps.hasPermission('content:link:update')")
    public ResponseResult updateLink(@RequestBody LinkDto linkDto){
        if (!StringUtils.hasText(linkDto.getName())){
            return ResponseResult.errorResult(500,"名称不能为空");
        }
        if (!StringUtils.hasText(linkDto.getLogo())){
            return ResponseResult.errorResult(500,"logo地址不能为空");
        }
        if (!StringUtils.hasText(linkDto.getAddress())){
            return ResponseResult.errorResult(500,"网站地址不能为空");
        }
        if (!StringUtils.hasText(linkDto.getStatus())){
            return ResponseResult.errorResult(500,"状态不能为空");
        }
        Link link = BeanCopyUtils.copyBean(linkDto, Link.class);
        linkService.updateById(link);
        return ResponseResult.okResult();
    }

    @PutMapping("/changeLinkStatus")
    @PreAuthorize("@ps.hasPermission('content:link:audit')")
    public ResponseResult changeLinkStatus(@RequestBody ChangeLinkStatusDto linkDto){
        Link link = linkService.getById(linkDto.getId());
        link.setStatus(linkDto.getStatus());
        linkService.updateById(link);
        return ResponseResult.okResult();
    }

    @GetMapping("/export")
    @PreAuthorize("@ps.hasPermission('content:link:export')")
    public void export(HttpServletResponse response){
        try {
            //设置下载文件的请求头
            WebUtils.setDownLoadHeader("友链.xlsx",response);
            //获取需要导出的数据
            List<Link> links = linkService.list();
            List<LinkVo> linkVos = BeanCopyUtils.copyBeanList(links, LinkVo.class);
            //把数据写入到Excel中
            EasyExcel.write(response.getOutputStream(), LinkVo.class).autoCloseStream(Boolean.FALSE).sheet("友链导出")
                    .doWrite(linkVos);

        } catch (Exception e) {
            //如果出现异常也要响应json
            ResponseResult result = ResponseResult.errorResult(AppHttpCodeEnum.SYSTEM_ERROR);
            WebUtils.renderString(response, JSON.toJSONString(result));
        }
    }
}
